<?php
/**
 * Created by PhpStorm
 * User: Kong
 * Date: 2025/9/25
 * Time: 10:48
 */

namespace App\Service\Admin;

class PermissionService
{
    /**
     * 添加权限.
     */
    public static function createPermission(array $data)
    {
        if (!isset($data['pid']) || $data['pid'] == 0) {
            // 添加的是权限组名称
            return \App\Model\Permission::create($data);
        }
        $permissions = \App\Model\Permission::query()->where('controller', $data['controller'])
            ->where('action', $data['action'])
            ->where('http_method', $data['http_method'])
            ->get();
        if ($permissions->count() > 0) {
            throw new \Exception('已存在此权限');
        }
        // 保存
        $rt = \App\Model\Permission::create($data);
        if (!$rt) {
            throw new \Exception('添加失败');
        }
        return [];
    }
}